package com.example.flowermall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.flowermall.entity.Menu;
import com.example.flowermall.mapper.MenuMapper;
import com.example.flowermall.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoman
 * @since 2023-06-11
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> getAllMenu() {
        LambdaQueryWrapper<Menu> queryWrapper=new LambdaQueryWrapper<>();
        //1级
        queryWrapper.eq(Menu::getParentId,0);
        List<Menu> menuList = menuMapper.selectList(queryWrapper);
        if(menuList!=null){
            //2级
            for(Menu menu:menuList){
                LambdaQueryWrapper<Menu> subWrapper=new LambdaQueryWrapper<>();
                subWrapper.eq(Menu::getParentId,menu.getMenuId());
                List<Menu> subMenuList = menuMapper.selectList(subWrapper);
                menu.setChildren(subMenuList);
            }
        }
        return menuList;
    }

    @Override
    public List<Menu> getMenuListByUserList(Integer id) {
        //一级
        List<Menu> menuList = menuMapper.getMenuListByUserList(id, 0);
        if(menuList!=null){
            for(Menu menu:menuList){
                //二级
                List<Menu> subMenuList = menuMapper.getMenuListByUserList(id, menu.getMenuId());
                menu.setChildren(subMenuList);
            }
        }
        return menuList;
    }
}
